package com.smh.a06exerciseDemo2;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test5 {
    public static void main(String[] args) {
        //统计指定文件的数量

        HashMap<String, Integer> hm = new HashMap<>();
        File file = new File("day25-code\\src\\com\\smh\\File");

        method(file, hm, "txt", "jpg", "doc");
        Set<Map.Entry<String, Integer>> entries = hm.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

    }

    public static void method(File file, HashMap<String, Integer> hm, String... s) {
        if (file.isFile()) {
            for (String string : s) {
                if (file.getName().endsWith("." + string)) {
                    if (!hm.containsKey(string)) {
                        hm.put(string, 1);
                    } else {
                        hm.put(string, hm.get(string) + 1);
                    }
                }
            }
        } else {
            File[] files = file.listFiles();
            if (files != null) {
                for (File file1 : files) {
                    method(file1, hm, s);
                }
            }

        }
    }
}
